package Servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import ServerDB.MapFileServer;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
 * Servlet :: Show files of a folder
 * @author Guy & Liel
 *
 */
public class FilesInFolderServlet  extends HttpServlet
{

	//NOTE: we using two session, the first is of Hibernate and the second is for session of email
	private static final long serialVersionUID = 1L;
	/**
	 * Request a folder id and get all the files under this folder
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.println("<HTML>");
		out.println("<HEAD>");
		System.out.println("I am here!!!!!!!!!");
		String folderID=request.getParameter("FolderID").toString();
		HttpSession httpSession=request.getSession(true);
		httpSession.setAttribute("FolderID", folderID);
		System.out.println(folderID);
		int intFolderID=Integer.parseInt(folderID);
		File file=new File("C:\\ourDropBox\\hibernate.cfg.xml");
		SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
		org.hibernate.classic.Session sessionHibernate=sessionFactory.openSession();
		String strQuery="FROM MapFileServer e WHERE e.folderId=:folderIdQry";
		Query query=sessionHibernate.createQuery(strQuery);
		query.setParameter("folderIdQry", intFolderID);
		System.out.println("After SQL line2");
		@SuppressWarnings("unchecked")
		List<MapFileServer> listFiles=query.list();

		System.out.println("my file is");
		httpSession=request.getSession(true);
		httpSession.setAttribute("Files", listFiles);
		out.println("<meta HTTP-EQUIV='REFRESH' content='0; url=filesInFolder.jsp'>");
		out.println("</HEAD>");
		out.println("<BODY>");
		out.println("</BODY>");
		out.println("</HTML>");
		out.close();


	}	
}

